#ifndef IVEFSTREAMHANDLER_H
#define IVEFSTREAMHANDLER_H

#include <iostream>

#include <QtCore>
#include <QtXml>
#include <QtNetwork>
#include <QMetaType>

#ifdef HAVE_ZLIB
#include "zlib.h"
#endif

#include "IVEFParser.h"

#define BUFFER_SIZE 1024*4

typedef std::function<void(const std::string&)> netErrorCallback;

class IVEFStreamHandler : public QObject {
    Q_OBJECT

public:
    IVEFStreamHandler(ivef::Parser *parser);
    ~IVEFStreamHandler();

    void sendObjectDatas(const ivef::ObjectDatas &datas);

    void sendLoginResponse();
    void setnetErrorCallback(netErrorCallback cb);
public slots:
    void slotStatisticsTimerFired();
    void slotDisconnected();
    void slotReadyRead();
    void slotConnected();
    void displayError(QAbstractSocket::SocketError socketError);
    void connectToServer(QString host, int port, QString user, QString password,
        QString logFileName, bool slipstream, bool statistics);
    void ping();

    void sendObjectDatasSlot(const ivef::ObjectDatas &datas);
Q_SIGNALS:
    void sendObjectDatasSignal(const ivef::ObjectDatas &datas);

private:
    void sendRawData(const QByteArray &data);

    QTcpSocket  *m_tcpSocket;
    QTextStream *m_log;

    QString m_user;
    QString m_password;

    QByteArray m_buffer;
#ifdef HAVE_ZLIB
    z_stream m_strm;
#endif
    bool m_slipstream;
    bool m_statistics;
    long m_bytesIn;
    long m_bytesOut;

    ivef::Parser *m_parser;

    netErrorCallback m_net_error_cb;
};

#endif
